package com.paic.mhis.hcpms.assessment.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.assessment.biz.service.AssessmentIndicatorService;
import com.paic.mhis.hcpms.assessment.dto.IndicatorConfigInfoDTO;
import com.paic.mhis.hcpms.assessment.dto.IndicatorConfigTreeDTO;
import com.paic.mhis.hcpms.assessment.dto.IndicatorHomeInfoDTO;
import com.paic.mhis.hcpms.quota.indicatorconfig.dto.IndicatorTreeDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

@Controller
public class AssessmentIndicatorController extends BaseRest {

	@BizServicesProxy(beanName = "assessmentIndicatorService")
	private AssessmentIndicatorService assessmentIndicatorService;

	@RequestMapping(value = "/assessment/indicator/queryIndicatorHomePage.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryIndicatorHomePage(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		List<IndicatorConfigTreeDTO> list = assessmentIndicatorService
				.queryIndicatorHomeTree(user.getUsername());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		return map;
	}
	
	@RequestMapping(value = "/assessment/indicator/queryIndicatorInfo.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryIndicatorConfig(HttpServletRequest request) {
		IndicatorHomeInfoDTO rstBean = assessmentIndicatorService
				.queryIndicatorConfig(request.getParameter("indicatorCode"),
						request.getParameter("regionCode"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", rstBean);
		return map;
	}
	
	@RequestMapping(value = "/assessment/indicator/toSaveIndicatorConfig.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> toSaveIndicatorConfig(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
//			JSONObject transferJson = JSON.parseObject(str);
//			transferJson.put("createdBy", user.getUsername());
//			transferJson.put("updatedBy", user.getUsername());
			List<IndicatorConfigInfoDTO> infoList = new ArrayList<IndicatorConfigInfoDTO>();  
			JSONArray array = JSON.parseArray(str);
			for (int i = 0; i < array.size(); i++) {
				JSONObject tempJson = array.getJSONObject(i);
				IndicatorConfigInfoDTO dto = JSONObject.toJavaObject(tempJson,
						IndicatorConfigInfoDTO.class);
				infoList.add(dto);
			}
			result = assessmentIndicatorService.saveIndicatorConfig(infoList,user);
			
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
	
	@RequestMapping(value = "/assessment/indicator/saveIndicatorInfo2.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> saveIndicatorConfig(HttpServletRequest request) {
		Map<String, Object> map = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		Map<String, Object> result = null;
		return result;
	}
}
